/**
 * 通用混合
 * */
import { postApi } from '@/api'
export default {
	methods: {
		// 当 $route 更新时触发
		goCommodityDetailServe(child) {
			const params = {
				skuId: child.skuId,
				supplierShopId: child.supplierShopId,
			}
			postApi(DYC_URL.ucc_noauthQueryGoodsDetails, params).then((res) => {
				let name = 'serveDetail'
				let url = this.$router.resolve({
					name: name,
					query: {
						skuId: res.commdInfo.skuInfo[0].skuId,
						supplierShopId: res.commdInfo.skuInfo[0].supplierShopId,
						supplierId: res.commdInfo.supplierId,
						skuSource: res.commdInfo.skuInfo[0].skuSource,
						commodityId: res.commdInfo.commodityId,
						vendorId: res.commdInfo.vendorId,
						erpSpuCode: res.commdInfo.skuInfo[0].extSpuId,
						productLevel: res.commdInfo.skuInfo[0].productLevel,
						skuForm: res.commdInfo.skuInfo[0].skuForm,
						skuClass: 1,
					},
				})
				window.open(url.href, '_blank')
			})
		},
		goGoodsDetail(child, parentOrder) {
			const params = {
				skuId: child.skuId,
				supplierShopId: child.supplierShopId,
			}
			postApi(DYC_URL.dy_queryGoodsDetails, params).then((res, code) => {
				// if (res.commodityPojo == null && res.respDesc == '该商品不存在') {
				//   this.$router.push({
				//     name: '404',
				//   });
				// }
				let skuData = ''
				let skuInfo = ''
				if (res) {
					if (res.commodityPojo.skuList && res.commodityPojo.skuList.length) {
						skuData = res.commodityPojo.skuList.filter(
							(item) => item.skuId == child.skuId
						)
						skuInfo = skuData[0]
					}
					if (res.commodityPojo.skuList[0].skuClass == 0) {
						let url = this.$router.resolve({
							name: 'unagrdetail',
							query: {
								skuId: child.skuId,
								supplierShopId: child.supplierShopId,
								supplierId: res.commodityPojo.supplierId,
								skuSource: skuInfo.skuSource,
								commodityId: res.commodityPojo.commodityId,
								vendorId: res.commodityPojo.vendorId,
								skuForm: skuInfo.skuForm,
							},
						})
						window.open(url.href, '_blank')
					} else {
						let url = this.$router.resolve({
							name: 'serveDetail',
							query: {
								skuId: child.skuId,
								supplierShopId: child.supplierShopId,
								supplierId: res.commodityPojo.supplierId,
								skuSource: skuInfo.skuSource,
								commodityId: res.commodityPojo.commodityId,
								vendorId: res.commodityPojo.vendorId,
								skuForm: skuInfo.skuForm,
							},
						})
						window.open(url.href, '_blank')
					}
				}
			})
		},
		getReceiveEmailList(contactPhone, detail) {},
		// 获取发票抬头--新-前台下单
		getInvoiceListNew() {
			const user_info = JSON.parse(localStorage.getItem('userData')).userInfo
			let params = {
				orgNameWeb: user_info.companyName,
			}
			postApi(DYC_URL.dyc_qryEnterpriseInvoiceListNew, params).then((res) => {
				console.log(res, 'res========================获取新发票抬头')
				if (res) {
					this.invoiceList = res.rows
				}
			})
		},
		// 获取发票抬头
		getInvoiceList() {
			let params = {
				invoiceType: this.invoiceBO.invoiceType,
				titleType: this.invoiceBO.invoiceTitle == 1 ? 'COMPANY' : 'PERSON',
				orgIdWeb: this.addFormData.purNo,
			}
			postApi(DYC_URL.dyc_qryAccountInvoiceListPage, params).then((res) => {
				if (res) {
					this.diITem = JSON.stringify(res.rows)
					this.invoiceList = res.rows
					this.invoiceBO.invoiceSn = this.invoiceList[0]
						? this.invoiceList[0].invoiceId
						: ''
					this.detail = this.invoiceList[0] ? this.invoiceList[0] : {}

					let phone = ''
					if (this.invoiceList.length > 0) {
						phone = this.invoiceList[0].contactPhone
							? this.invoiceList[0].contactPhone
							: this.invoiceList[0].phone
					} else {
						phone = JSON.parse(localStorage.getItem('userData')).userInfo.cellphone
					}
					let orgId = JSON.parse(localStorage.getItem('userData')).userInfo.orgId
					let params = {
						tel: phone,
						orgId: orgId ? orgId : '',
					}
					console.log('发票电子邮箱列表参数', params)
					postApi(DYC_URL.dy_qryInvoiceAddressList, params).then((resd) => {
						console.log('发票电子邮箱列表>>>>>>>>>>>>>>>>>>>>>>>>>', resd)
						if (resd.rows.length > 0) {
							resd.rows.forEach((item) => {
								if (item.mainFlag == 1) {
									console.log('主邮箱:', item.elcInvoiceEmail)
									this.detail.receiveEmail = item.elcInvoiceEmail
								}
							})
							if (!this.detail.receiveEmail) {
								this.detail.receiveEmail = res.rows[0].elcInvoiceEmail
							}
							this.receiveEmailListOrder = resd.rows.filter(
								(item) => item.elcInvoiceEmail && item.elcInvoiceEmail.trim() != ''
							)
						} else {
							setTimeout(() => {
								console.log('没有电子邮箱', JSON.parse(this.diITem))
								let dtDetail = JSON.parse(this.diITem)[0]
								if (dtDetail && dtDetail.receiveEmail) {
									this.detail.receiveEmail = dtDetail.receiveEmail
									dtDetail.elcInvoiceEmail = dtDetail.receiveEmail
									this.receiveEmailListOrder = []
									this.receiveEmailListOrder.push(dtDetail)
								} else {
									this.detail.receiveEmail = ''
									this.receiveEmailListOrder = []
								}
								console.log(this.receiveEmailListOrder, '12121=====121212')
							}, 500)
						}
					})
				}
			})
		},
		getReceiveEmailListNext(contactPhone, receiveDetail) {
			let orgId = JSON.parse(localStorage.getItem('userData')).userInfo.orgId
			let params = {
				tel: contactPhone,
				orgId: receiveDetail && receiveDetail.orgId ? receiveDetail.orgId : orgId,
			}
			console.log('发票电子邮箱列表参数', params)
			postApi(DYC_URL.dy_qryInvoiceAddressList, params).then((res) => {
				console.log('发票电子邮箱列表>>>>>>>>>>>>>>>>>>>>>>>>>', res)
				if (res.rows.length > 0) {
					res.rows.forEach((item) => {
						if (item.mainFlag == 1) {
							this.placeAnOrdersForm.receiveEmail = item.elcInvoiceEmail
							this.receiveDetail.receiveEmail = item.elcInvoiceEmail
						}
					})
					if (!this.placeAnOrdersForm.receiveEmail) {
						this.placeAnOrdersForm.receiveEmail = res.rows[0].elcInvoiceEmail
						this.receiveDetail.receiveEmail = res.rows[0].elcInvoiceEmail
					}
					this.receiveEmailLists = res.rows.filter(
						(item) => item.elcInvoiceEmail && item.elcInvoiceEmail.trim() != ''
					)
				} else {
					if (receiveDetail && receiveDetail.receiveEmail) {
						this.placeAnOrdersForm.receiveEmail = receiveDetail.receiveEmail
						this.receiveDetail.receiveEmail = receiveDetail.receiveEmail
						receiveDetail.elcInvoiceEmail = receiveDetail.receiveEmail
						this.receiveEmailLists = []
						this.receiveEmailLists.push(receiveDetail)
					} else {
						this.placeAnOrdersForm.receiveEmail = ''
						this.receiveDetail.receiveEmail = ''
						this.receiveEmailLists = []
					}
				}
				console.log(this.receiveEmailLists, 'receiveEmailList=======receiveEmailList')
			})
		},
		// 获取发票抬头
		getInvoiceListNext() {
			let params = {
				invoiceType: this.placeAnOrdersForm.invoiceType,
				titleType: this.placeAnOrdersForm.invoiceTitle == 1 ? 'COMPANY' : 'PERSON',
				orgIdWeb: this.placeAnOrdersForm.purNo,
			}
			postApi(DYC_URL.dyc_qryAccountInvoiceListPage, params).then((res) => {
				if (res) {
					this.invoiceList = res.rows
					this.placeAnOrdersForm.invoiceSn = this.invoiceList[0]
						? this.invoiceList[0].invoiceId
						: ''
					this.receiveDetail = this.invoiceList[0] ? this.invoiceList[0] : {}

					if (this.invoiceList.length > 0) {
						this.getReceiveEmailListNext(
							this.invoiceList[0].contactPhone,
							this.receiveDetail
						)
					} else {
						let userCellphone = JSON.parse(localStorage.getItem('userData')).userInfo
							.cellphone
						this.getReceiveEmailListNext(userCellphone, this.receiveDetail)
					}
					this.skeletonLoading = false
				} else {
					this.skeletonLoading = false
				}
			})
		},
		// 设置默认地址-发票
		setDefaultInvoice(row) {
			let params = {
				operType: '2',
				mainFlag: '1',
				id: row.id,
			}
			postApi(DYC_URL.pay_updateInvoiceAddress, params).then((res) => {
				if (res) {
					this.$Message.success({
						// content: res.respDesc ? res.respDesc : '设置默认收货地址成功！'
						content: '设置默认发票邮寄地址成功！',
					})
					this.getInvoiceAddressList()
				}
			})
		},
		// 删除发票邮寄地址
		delAttrInvoice(row) {
			this.$Modal.confirm({
				title: '提示',
				content: '确定要删除吗?',
				onOk: () => {
					let params = {
						operType: '3',
						ids: [row.id],
					}
					this.$Spin.show()
					postApi(DYC_URL.pay_operInvoiceAddress, params).then((res) => {
						this.$Spin.hide()
						if (res) {
							this.$Message.success({
								content: res.respDesc ? res.respDesc : '删除成功！',
							})
							this.getInvoiceAddressList()
						}
					})
				},
			})
		},
		// 获取邮寄地址
		getInvoiceAddressList() {
			let username = JSON.parse(localStorage.getItem('userData')).userInfo
			let params = {
				// orgIdWeb: username.companyId
				orgIdWeb: this.addFormData.purNo,
			}
			this.$Spin.show()
			postApi(DYC_URL.pay_qryInvoiceAddressListPage, params).then((res) => {
				if (res) {
					this.$Spin.hide()
					if (res.rows && res.rows.length) {
						this.receivingInvoicInfo = res.rows
						if (this.visibleInnvio === true) {
							this.receivingInvoicInfoShow = JSON.parse(
								JSON.stringify(this.receivingInvoicInfo)
							)
							this.$nextTick(() => {
								this.chooseReceiveInvoic.postAddress = res.rows[0]
								this.chooseReceiveInvoic.id = res.rows[0].id
							})
						} else {
							this.chooseReceiveInvoic.postAddress = res.rows[0]
							this.chooseReceiveInvoic.id = res.rows[0].id
							this.receivingInvoicInfoShow = JSON.parse(
								JSON.stringify(this.receivingInvoicInfo)
							).splice(0, 1)
						}
					} else {
						this.receivingInvoicInfo = []
						this.receivingInvoicInfoShow = []
						this.chooseReceiveInvoic.id = ''
					}
				}
			})
		},
		// 获取邮寄地址
		getInvoiceAddressListNext() {
			let username = JSON.parse(localStorage.getItem('userData')).userInfo
			let params = {
				// orgIdWeb: username.companyId
				orgIdWeb: this.placeAnOrdersForm.purNo,
			}
			this.$Spin.show()
			postApi(DYC_URL.pay_qryInvoiceAddressListPage, params).then((res) => {
				if (res) {
					this.$Spin.hide()
					if (res.rows && res.rows.length) {
					}
				}
			})
		},
		// 新增邮寄地址
		addInvoice() {
			this.$refs.invoiceReceAddForm.addInvoiceModel = true
			this.$refs.invoiceReceAddForm.formValidate.orgName = this.orgName
			this.$refs.invoiceReceAddForm.formValidate.orgIdWeb = this.orgIdWeb
		},
		toggleInnvio(from) {
			this.visibleInnvio = !this.visibleInnvio
			/**
			 * @param 如果是从下单页面过来的 则需要替换key值
			 **/

			if (from === 'placeAnOrder') {
				this.visibleInnvio
					? (this.receivingInvoicInfoShow = this.receivingInvoicInfo)
					: (this.receivingInvoicInfoShow = JSON.parse(
							JSON.stringify(this.receivingInvoicInfo)
					  ).slice(0, 1))
			} else {
				this.visibleInnvio
					? (this.receivingInvoicInfoShow = this.receivingInvoicInfo)
					: (this.receivingInvoicInfoShow = JSON.parse(
							JSON.stringify(this.receivingInvoicInfo)
					  ).splice(0, 1))
			}
		},
		// 获取账户信息
		getMerchantDetail() {
			postApi(DYC_URL.pay_qryMerchantDetail, {}).then((res) => {
				if (res) {
					this.accountDetail = res
				}
			})
		},
		// 查询账期详情
		getErpCustomerAccountPeriod() {
			let params = {
				merchantNo: this.getUserInfo.orgId,
				ecommerceContact: this.getUserInfo.userId,
			}
			postApi(DYC_URL.order_qryErpCustomerAccountPeriod, params).then((res) => {
				if (res) {
					if (res.data.records.length > 0) {
						this.erpCustomerAccountPeriod = res.data.records[0]
					} else {
						this.erpCustomerAccountPeriod = {}
					}
				}
			})
		},
		// 获取客户预存款列表
		getCustomerDepositList(orgId) {
			let params = {
				customerId: orgId || this.getUserInfo.orgId,
			}
			postApi(DYC_URL.order_qryCustomerDepositList, params).then((res) => {
				if (res) {
					if (res.rows && res.rows.length && res.rows[0].vipCardNo) {
						this.getCustomerDepositBalance(res.rows[0].vipCardNo, orgId)
					}
				}
			})
		},
		// 客户预存款余额
		getCustomerDepositBalance(cardNo, orgId) {
			let params = {
				merchantNo: orgId || this.getUserInfo.orgId,
				cardNo,
			}
			postApi(DYC_URL.order_qryCustomerDepositBalance, params).then((res) => {
				if (res) {
					this.customerDepositBalance = res
					this.customerDepositBalance['cardNo'] = cardNo
				}
			})
		},
		async toEchat(item) {
			console.log('toEchat==================', item)
			if (!localStorage.getItem('userData')) {
				this.$Message.warning('请先登录')
				return
			}
			const userData = localStorage.getItem('userData')
			if (userData) {
				let params = {
					shopId: item.orderItemList[0].supplierShopId
						? item.orderItemList[0].supplierShopId
						: '',
					supplierId: item.supNo ? item.supNo : '',
				}
				postApi(DYC_URL.noauth_qryShopSetting, params).then((res) => {
					console.log(res, 'res=====================res')
					if (res) {
						if (res.shopSetting) {
							window.open(res.shopSetting.wechatCs)
							this.shopSetting = res.shopSetting
						} else {
							window.open(res.platformSetting.wechatCs)
							this.shopSetting = res.platformSetting
						}
					}
				})
				// const userInfo = JSON.parse(userData).userInfo;
				// const { name } = userInfo;
				// // let visEvt = {
				// //   'eventId': 'cook1005',
				// //   'title': '2016秋冬新品复古欧式感大花苞袖宽松毛衣',
				// //   'content': '<div style=\'color:#666;line-height:20px\'>原价：<span style=\'text-decoration:line-through\'>¥414.99</span></div><div style=\'color:#666;line-height:20px\'>促销：<span style=\'color:red\'>¥211.64</span></div><div style=\'color:#666;line-height:20px\'>运费：<span style=\'color:#ccc\'>卖家承担运费</span></div>',
				// //   'imageUrl': 'https://app.echatsoft.com/getUrl/images/0.jpg',
				// //   'urlForVisitor': 'http(\'https://app.echatsoft.com/web/html/demoMall/url/staffUrl/myproduct/?eventId=cook1000\',\'inner\')',
				// //   'urlForStaff': 'http(\'https://app.echatsoft.com/web/html/demoMall/url/staffUrl/myproduct/?eventId=cook1000\',\'inner\')',
				// //   'memo': '评价（299）',
				// //   'visibility': 1,
				// //   'customizeMsgType': 1
				// // };
				// // const url = `https://es.echatsoft.com/visitor/pc/chat.html?companyId=530696&visEvt=#{${visEvt}}`;
				// // encodeURI(url)
				// // window.open(decodeURI(url), '_blank');
				// let replyMsg = `<xml><name><![CDATA[${name}]]></name><visitorType><![CDATA[1]]></visitorType></xml>`;
				// const metaData = await postApi(DYC_URL.common_echatCustomerServiceSystemAccess, { replyMsg });
				// if (metaData) {
				//   window.open(
				//     `https://vs.rainbowred.com/visitor/pc/chat.html?companyId=530696&metaData=${metaData.metaData}`,
				//     '', 'width=900,height=600,Resizable=yes'
				//   );
				// }
			}
		},
		// 获取年 月 日时分秒
		getymd(dateStr, type) {
			let date = dateStr - 0
			if (type == 'YY') {
				return new Date(date).getFullYear()
			} else if (type == 'DD') {
				return parseInt(date / (60 * 60 * 24 * 1000))
			} else if (type == 'HH') {
				return parseInt((date / (60 * 60 * 1000)) % 24)
			} else if (type == 'mm') {
				return parseInt((date / (60 * 1000)) % 60)
			} else if (type == 'SS') {
				return parseInt((date / 1000) % 60)
			}
		},
	},
}
